package com.example.springboot.controller.视图解析;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.bean.Login;
import com.example.springboot.bean.LoginUser;
import com.example.springboot.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * templates这个静态文件  都是经过处理的 html文件的访问不需要加   .html
 */
@Controller
public class thymeleafController {


    @Autowired
    LoginService loginService;

    @Autowired
    StringRedisTemplate redisTemplate;

    @GetMapping("/login")
    public String loginDemo(){
        return "login";
    }

    @PostMapping("/login")
    public String index(LoginUser loginUser, HttpSession session, Model model){

        if("张蛟龙".equals(loginUser.getUsername()) &&
                StringUtils.hasLength(loginUser.getPassword())){//判断是否有长度
            //登录成功的用户保存一下 以后使用方便
            session.setAttribute("loginUser",loginUser);
            return "redirect:/main.html";//重定向 防止表单重复提交
        }else {
            model.addAttribute("msg","账号密码错误");
            return "login";//回到登录界面
        }
    }

    @GetMapping("/main.html")
    public String goIndex(Model model){
        //判断是否登录  太麻烦了  使用拦截器
        //详见com.example.springboot.config.addInterceptors()
        //和com.example.springboot.config拦截器

        ValueOperations<String,String> operations =
                redisTemplate.opsForValue();
        String table = operations.get("/table");
        String filesc = operations.get("/filesc");
        String userLongs = operations.get("/userLongs");
        String logins = operations.get("/logins");

        model.addAttribute("redisTable",table);
        model.addAttribute("redisFilesc",filesc);
        model.addAttribute("redisUserLongs",userLongs);
        model.addAttribute("redisLogins",logins);
        return "main";
    }


    @GetMapping("/table")
    public String table(
            Model model
//            @RequestParam(value = "pn",defaultValue = "1")Integer pn, Model model
    ){
         List<LoginUser> loginUsers = Arrays.asList(
                 new LoginUser("张三", "333"),
                 new LoginUser("李四", "444"),
                 new LoginUser("王五", "555"),
                 new LoginUser("赵六", "666"),
                 new LoginUser("钱七", "777"));
        model.addAttribute("loginUsers",loginUsers);

//        if(loginUsers.size()>3){
//            throw new 自定义异常的自定义异常处理();
//        }
//        List<Login> list = loginService.list();
//        Page<Login> logins = new Page<>(pn, 2);
//        Page<Login> page = loginService.page(logins, null);
//        model.addAttribute("page",page);

        return "table";
    }

    @GetMapping("/user/delete/{id}")
    public String deleteLogin(@PathVariable("id") Long id,
                              @RequestParam(value = "pn" ,defaultValue = "1") Integer pn,
                              RedirectAttributes ra){// 重定向携带的数据
        loginService.removeById(id);
        ra.addAttribute("pn",pn);
        return "redirect:/dynamic_table";//重定向
    }

}
